package com.springboot.system.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.springboot.system.domain.auth.UserInfo;
import com.springboot.system.domain.auth.UserInfoVO;
import com.springboot.common.utils.StringUtils;
import com.springboot.core.jdbc.BaseDao;
import com.springboot.core.web.mvc.Page;

/**
 * 用户数据持久层实现类
 * 
 * @ClassName: UserDao
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 252956
 * @date 2021年1月7日 上午9:39:04
 *
 */
@Repository
public class UserDao extends BaseDao {

	public Page<UserInfo> selectPage(UserInfoVO user) {
		List<UserInfo> list = super.select(
				getSqlPageHandle().handlerPagingSQL(userPageSql(user, 0), user.getPage(), user.getLimit()), null,
				UserInfo.class);
		int count = super.getJdbcTemplate().queryForObject(userPageSql(user, 1), null, Integer.class);
		Page<UserInfo> page = new Page<UserInfo>(list, count, user.getLimit(), user.getPage());
		return page;
	}

	private String userPageSql(UserInfoVO user, int type) {
		StringBuilder sql = new StringBuilder();
		if (type == 0) {
			sql.append(
					" select  u.uid,u.name,u.vsername,u.password,u.mobile,u.email,u.create_time as createTime,u.status,u.dept_id as deptId,d.name as deptName,u.job_id as jobId,j.name as jobName from t_sys_user u  ");
			sql.append(" left join t_sys_dept d on d.id=u.dept_id ");
			sql.append(" left join t_sys_job j on j.id=u.job_id ");
		} else {
			sql.append(" select count(*) from t_sys_user u  ");
			sql.append(" left join t_sys_dept d on d.id=u.dept_id ");
			sql.append(" left join t_sys_job j on j.id=u.job_id ");
		}
		sql.append(" where 1=1");

		if (StringUtils.isNotBlank(user.getName())) {
			sql.append(" and u.name like '%").append(user.getName().trim() + "%' ");
		}
		if (StringUtils.isNotBlank(user.getVserName())) {
			sql.append(" and u.vsername like '%").append(user.getVserName().trim() + "%' ");
		}
		if (StringUtils.isNotBlank(user.getMobile())) {
			sql.append(" and u.mobile like '%").append(user.getMobile().trim() + "%' ");
		}
		if (user.getDeptId() != null && user.getDeptId() != 2) {
			sql.append(" and u.dept_id=" + user.getDeptId() + "");
		}
		if (user.getStatus() != null) {
			sql.append(" and u.status=" + user.getStatus() + "");
		}
		if (type == 0) {
			if (StringUtils.isNotBlank(user.getSidx())) {
				if ((user.getSord().trim().equalsIgnoreCase("asc"))) {
					sql.append(" order by " + user.getSidx().split(" ")[0] + " asc");
				} else {
					sql.append(" order by " + user.getSidx().split(" ")[0] + " desc");
				}
			} else {
				sql.append(" order by create_time asc");
			}
		}
		return sql.toString();
	}

	public UserInfo select(String id) {
		StringBuilder sql = new StringBuilder();
		sql.append(
				"select u.uid,u.name,u.password,u.vsername,u.mobile,u.create_time as createTime,u.status,u.dept_id as deptId,d.name as deptName,u.job_id as jobId,j.name as jobName from t_sys_user u");
		// sql.append(" left join t_sys_user_role ur on ur.user_id=u.uid");
		// sql.append(" left join t_sys_role r on r.id=ur.role_id");
		sql.append(" left join t_sys_dept d on d.id=u.dept_id");
		sql.append(" left join t_sys_job j on j.id=u.job_id ");
		sql.append(" where u.uid='" + id + "'");
		List<UserInfo> list = super.select(sql.toString(), null, UserInfo.class);
		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	public int updateStatus(String uid, int status) {
		StringBuilder sql = new StringBuilder();
		sql.append("update t_sys_user set status=? where uid=?");
		return super.insertOrUpdateOrDelete(sql.toString(), new Object[] { status, uid });
	}

}
